<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/5/7
  Time: 10:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>

</head>

<body>
<div class="x-body">

    <form class="layui-form">
        <div class="layui-form-item">
            <label  class="layui-form-label">
                <span class="x-red">*</span>文件名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="fileName" name="fileName" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>文件地址
            </label>
            <div class="layui-input-inline">
                <input type="text" id="fileUrl" name="fileUrl" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>

            <button  class="layui-btn" type="button" id="upload">
                上传文件
            </button>

        </div>



        <div class="layui-form-item">
            <label  class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="add" lay-submit type="submit">
                增加
            </button>
        </div>
    </form>
</div>
<script>
    //使用layui完成表单提交
    layui.use(['form','layer','upload'],function () {
        //定义layui对象
        $ = layui.jquery;
        //定义表单对象
        var form = layui.form,layer = layui.layer;
        //创建文件上传对象
        var up = layui.upload;
        //文件上传的点击事件 -- 文件上传
        var upload = up.render({
            //绑定元素
            elem:'#upload',
            url:'/file/upload',
            accept:'file',
            done:function (res) {
                console.log(res);
                //数据回显
                $("#fileName").val(res.data.fileName);
                $("#fileUrl").val(res.data.servicePath);
            }
        });

        //将文件内容存入数据库
        form.on('submit(add)',function (data) {
            console.log(data);//表单所有内容
            //发Ajax
            $.ajax({
                url:"/myFileAjax/insert",
                type:"post",
                dataType:"json",
                data:data.field,
                success:function (rs) {
                    layer.alert(rs.message,{icon:6},function () {
                        if (rs.code == 200) {
                            x_admin_close();
                            parent.window.loadData();
                        }
                    })
                }
            })
            return false; //阻止表单跳转刷新
        })
    })

</script>

</body>

</html>
